/**
 * Created by rong on 16/9/6.
 */
require.config(require_config);
define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var goods_id = getUrlPath(3);
    var promotion_active_id = getUrlPath(4);
    var id = getUrlPath(5);
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            this.fetch();
            this.event();
        },
        fetch: function () {
            $.ajax({
                url: _global.url.api + 'site/sign_up',
                data: {goods_id: goods_id},
                type: 'get',
                async: true,
                success: function (res) {
                    console.log("res ,", res);
                    if (res.code == 0) {
                        var html = template('data-item', res.data);
                        $("#data-list").html(html);
                    }
                }
            });

        },
        event: function () {
            this.data = {};
            var _this = this;

            $('input,select,textarea').on('change', function (e) {
                _this.change(e);
            });
            //上传图片
            $('body').on('click', '.btn-upload', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        img.item = item;
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });

            //$("#btn-save").on("click", function () {
            $('body').on('click', '.btn-save', function () {
                _this.data.goods_id = goods_id;
                _this.data.promotion_active_id = promotion_active_id;
                _this.data.id = id;
                _this.data.title = $('#title').val();
                _this.data.img = $("input[name='imgs']").val();
                if (_this.data.title == '' || _this.data.title == 'undefined') {
                    Alert({msg: '请输入展示标题', type: 'error'});
                    return false;
                }

                if (_this.data.img == '' || _this.data.img == 'undefined') {
                    Alert({msg: '请上传展示图片', type: 'error'});
                    return false;
                }

                if (new Model({
                        url: _global.url.api + 'site/sign_up',
                        data: _this.data
                    }).put()) {
                    //location.href = '/site/groupbuy';
                }
            });
            //保存
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });

        },
        imgPreview: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view' + img.item).html(template('img-tpl', img));
            }
        }
    };

    new index();
});